package com.legendframework.core;

import com.legendframework.core.cmd.CommandManager;
import com.legendframework.core.cmd.CommandRootConfig;
import com.legendframework.core.log.LegendLogger;
import com.legendframework.core.log.Logger;
import com.legendframework.core.utils.BukkitVersionUtils;
import com.legendframework.core.support.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;

public abstract class AbstractLegendPlugin extends JavaPlugin implements ILegendPlugin {

    protected Store store;

    protected Logger log;

    public AbstractLegendPlugin() {
        this.store = createStore();
        this.log = createLogger();
    }

    /**
     * 构建框架容器
     * @return
     */
    protected abstract Store createStore();

    /**
     * 初始化主类
     */
    protected void init() {}

    /**
     * 插件被Bukkit启用的时候执行
     */
    @Override
    public void onEnable() {
        init();
        registerCommand();
        start();
    }

    /**
     * 构建日志对象
     * @return
     */
    protected Logger createLogger(){
        return new LegendLogger(this.getName(),getStore());
    }

    /**
     * 获取插件的框架核心容器
     * @return
     */
    public Store getStore(){
        return store;
    }

    /**
     * 插件被Bukkit载入的时候执行
     * 此时还未执行onEnable方法
     *
     */
    @Override
    public void onLoad() {
        load();
    }

    /**
     * 插件被Bukkit卸载的时候执行
     */
    @Override
    public void onDisable() {
        end();
    }

    @Override
    public SaveFileName getSaveFileName() {
        return new SaveFileName(this.getName(),this.getName());
    }

    @Override
    public String getSavePath() {
        return this.getDataFolder().getParentFile().getAbsolutePath();
    }

    @Override
    public Logger getLog() {
        return log;
    }

    @Override
    public CommandRootConfig getCommandRootConfig() {
        String usageMessage = StringUtils.isNotEmpty(getRootCmd()) ? "输入: /" + getRootCmd() + " help 得到帮助" : null;
        return new CommandRootConfig("legendframework插件开发框架制造",usageMessage,null,null);
    }

    public final ClassLoader getMainClassLoader(){
        return this.getClassLoader();
    }

    @Override
    public CommandManager getCommandManager() {
        return getStore().getCommandManagerStore().getManagerPool().get(this.getClass());
    }

    /**
     * 注册指令
     */
    protected void registerCommand(){
        if (StringUtils.isNotEmpty(getRootCmd())) {
            CommandMap commandMap = BukkitVersionUtils.getCommandMap();
            CommandManager commandManager = getCommandManager();
            if (commandMap != null && isAutowiredRegister()) {
                PluginCommand pluginCommand = Bukkit.getPluginCommand(getRootCmd());
                if (pluginCommand == null) {
                    //尝试不进行编写plugin.yml配置文件中的指令配置而进行注册指令
                    PluginCommand command = BukkitVersionUtils.createCommand(getRootCmd(), this);
                    if (command != null) {
                        CommandRootConfig commandRootConfig = getCommandRootConfig();
                        if (commandRootConfig != null) {
                            command.setDescription(commandRootConfig.getDescription());
                            command.setUsage(commandRootConfig.getUsageMessage());
                            command.setPermission(commandRootConfig.getPermission());
                            command.setPermissionMessage(commandRootConfig.getPermissionMessage());
                        }
                        command.setExecutor(commandManager);
                        commandMap.register(getRootCmd(),command);
                        return;
                    }
                }
            }
            //如果你看源码看到了这里，请注意，框架已默认注册了一个指令，根指令名称为你实现的getRootCmd()方法返回值
            //如果自动注册指令失败，启动报错或者出现不兼容的情况，请务必再你插件的plugin.yml中添加一个项：commands:
            Bukkit.getPluginCommand(getRootCmd()).setExecutor(commandManager);
        }
    }

}
